import requests
from bs4 import BeautifulSoup
import pandas as pd

def get_movie_data(url, headers):
    response = requests.get(url, headers=headers)
    content = response.text

    # 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(content, "html.parser")
    movies = soup.select(".grid_view .item")

    # 提取所需信息
    movie_data = []
    for movie in movies:
        rank = movie.find("em").text
        title = movie.find("span", class_="title").text
        rating = movie.find("span", class_="rating_num").text
        movie_data.append([rank, title, rating])
        return movie_data

# 请求豆瓣电影Top 250页面
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"
}

# all_movie_data = []
# for i in range(0, 250, 25):
#     url = f"https://movie.douban.com/top250?start={i}"
#     all_movie_data.extend(get_movie_data(url, headers))
#
# # 将数据存入pandas DataFrame
# columns = ["排名", "电影名称", "评分"]
# df = pd.DataFrame(all_movie_data, columns=columns)
#
# # 将数据导出到Excel文件
# df.to_excel("豆瓣电影Top250.xlsx", index=False, engine='openpyxl')


def get_movie_data2(url, headers):
    response = requests.get(url, headers=headers)
    content = response.text

    # 使用BeautifulSoup解析页面内容
    soup = BeautifulSoup(content, "html.parser")
    movies = soup.select(".grid_view .item")

    # 提取所需信息
    movie_data = []
    for movie in movies:
        rank = movie.find("em").text
        title = movie.find("span", class_="title").text
        rating = movie.find("span", class_="rating_num").text
        content_span = movie.find("div", class_="star").find("span",string="2818220人评价")
        # content = movie.find("span", class_="").text
        print(content_span)
        exit()
        movie_data.append([rank, title, rating])
        return movie_data


url = f"https://movie.douban.com/top250"
get_movie_data2(url, headers)

